package com.webridge.plm.datautility;

import java.util.HashMap;
import java.util.List;

import org.apache.log4j.Logger;

import wt.fc.Persistable;
import wt.log4j.LogR;
import wt.part.WTPart;
import wt.part.WTPartMaster;
import wt.part.WTPartUsageLink;
import wt.util.WTException;

import com.ptc.core.components.descriptor.ModelContext;
import com.ptc.core.components.factory.AbstractDataUtility;
import com.ptc.core.components.rendering.guicomponents.TextDisplayComponent;
import com.webridge.plm.util.PartUtil;

/**
 * 获取Table对象的Version，Number,Name,Qty
 * 由于Datautility渲染时，getDataValue()是每个单元格都需要实时查询数值，在表格、树的结构比较复杂，数据量大时，会引起性能问题。因此，建议使用setModelData.
 * @author kira
 */
public class VersionDatautility extends AbstractDataUtility {
    
    
    private HashMap<Persistable,String> versionMap = new HashMap <Persistable,String>();
    private HashMap<Persistable,String> nameMap = new HashMap <Persistable,String>();
    private HashMap<Persistable,String> numberMap = new HashMap <Persistable,String>();
    private HashMap<Persistable,String> qtyMap = new HashMap <Persistable,String>();
    
    
    public static Logger LOGGER = LogR.getLogger(VersionDatautility.class.getName());
    
    @Override
    public Object getDataValue(String component_id, Object datum, ModelContext modelContext) throws WTException {
        LOGGER.debug("component_id:"+component_id +",datum:" + datum);

        modelContext.getDescriptorMode();
        TextDisplayComponent textDcom = new TextDisplayComponent("");
        String value = "";
        if ("number".equals(component_id)) {
            value = numberMap.get(datum);
        } else if ("name".equals(component_id)) {
            value = nameMap.get(datum);
        } else if ("Version".equals(component_id)) {
            value = versionMap.get(datum);
        }else if("qty".equals(component_id)){
            value = qtyMap.get(datum);

        }
        LOGGER.debug("value:" + value);
        textDcom.setValue(value == null ? "" : value);

        return textDcom;

    }
    
    
    /**
     * 实现这个方法，用来收集各Map的数据。
     */
    @Override
    public void setModelData(String paramString, List<?> paramList, ModelContext paramModelContext)
            throws WTException
          {
            if(paramList!=null){
                for(Object obj:paramList){
                    if(obj instanceof WTPart){
                        WTPart part = (WTPart) obj;
                        LOGGER.debug("setModelData Part:" + part.getNumber());
                        String version=part.getVersionIdentifier().getValue() + "." + part.getIterationIdentifier().getValue();
                        nameMap.put(part, part.getName());
                        numberMap.put(part, part.getNumber());
                        versionMap.put(part, version);
                    }else if(obj instanceof WTPartUsageLink){
                        WTPartUsageLink link = (WTPartUsageLink) obj;
                        WTPartMaster master = (WTPartMaster) link.getRoleBObject();
                        WTPart part=PartUtil.getLatestPartByMaster(master);
                        String version=part.getVersionIdentifier().getValue() + "." + part.getIterationIdentifier().getValue();
                        nameMap.put(link, part.getName());
                        numberMap.put(link, part.getNumber());
                        versionMap.put(link, version);
                        qtyMap.put(link,String.valueOf(link.getQuantity().getAmount()));
                        LOGGER.debug("setModelData WTPartUsageLink role B:" + link.getRoleBObject());
                    }
                }
            }else{
                LOGGER.debug("paramList is null.");

            }
        
        
        
          }
}
